package com.ruicar.afs.cloud.apply.archive.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.apply.archive.service.MortgageService;
import com.ruicar.afs.cloud.apply.business.service.TSysParamConfigService;
import com.ruicar.afs.cloud.archive.dto.MortgageDto;
import com.ruicar.afs.cloud.archive.feign.MortgageApiFeign;
import com.ruicar.afs.cloud.archive.result.ArchivedMortgageDto.ArchivedMortgageInfo;
import com.ruicar.afs.cloud.archive.result.ArchivedMortgageDto.ArchivedMortgageList;
import com.ruicar.afs.cloud.archive.result.ArchivedPageDto;
import com.ruicar.afs.cloud.archive.result.ArchivedResultDto;
import com.ruicar.afs.cloud.archive.service.ArchiveService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解抵押service实现类
 *
 * @author 耿继辉
 */
@Service
@AllArgsConstructor
public class MortgageServiceImpl implements MortgageService {

    private final MortgageApiFeign mortgageApiFeign;
    private final ArchiveService archiveService;
    private final TSysParamConfigService paramConfigService;

    /**
     * 获取token信息
     *
     * @return
     */
    public String getTokenInfo() {
//        String authorization = paramConfigService.getParamValue("archive", "token", "");
        String authorization = archiveService.getAuthorization();
        return authorization;
    }

    /**
     * 解抵押-解抵押列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse getMortgageList(MortgageDto dto) {

        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        try {
            ArchivedResultDto json =  this.mortgageApiFeign.getMortgageList(dto, headers);
            if(1==json.getSuccess()){
                ArchivedPageDto archivedPageDto = JSON.parseObject(JSON.toJSONString(json.getData()), ArchivedPageDto.class);
                List<ArchivedMortgageInfo> list = JSON.parseArray(JSON.toJSONString(archivedPageDto.getData()), ArchivedMortgageInfo.class);
                //分页处理
                IPage iPage = new Page();
                iPage.setRecords(list);
                iPage.setPages(archivedPageDto.getPageIndex());
                iPage.setSize(archivedPageDto.getPageSize());
                iPage.setTotal(archivedPageDto.getRecordsTotal());
                return IResponse.success(iPage);
            }else{
                return IResponse.fail(json.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail("接口异常，请联系管理员！");
        }
    }

    /**
     * 解抵押-解抵押列表模糊查询
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse matching(MortgageDto dto) {

        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        try {
            ArchivedResultDto json =  this.mortgageApiFeign.matching(dto, headers);
            if(1==json.getSuccess()){
                ArchivedPageDto archivedPageDto = JSON.parseObject(JSON.toJSONString(json.getData()), ArchivedPageDto.class);
                List<ArchivedMortgageInfo> list = JSON.parseArray(JSON.toJSONString(archivedPageDto.getData()), ArchivedMortgageInfo.class);
                //分页处理
                IPage iPage = new Page();
                iPage.setRecords(list);
                iPage.setPages(archivedPageDto.getPageIndex());
                iPage.setSize(archivedPageDto.getPageSize());
                iPage.setTotal(archivedPageDto.getRecordsTotal());
                return IResponse.success(iPage);
            }else{
                return IResponse.fail(json.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail("接口异常，请联系管理员！");
        }

    }

    /**
     * 解抵押-解抵押确认
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse sure(MortgageDto dto) {
        try {
            Map<String, String> headers = new HashMap<>(2);
            headers.put("Authorization", this.getTokenInfo());
            ArchivedResultDto json =  this.mortgageApiFeign.sure(dto, headers);
            if(1==json.getSuccess()){
                return IResponse.success(true);
            }else{
                return IResponse.fail(json.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail("接口异常，请联系管理员！");
        }

    }

    /**
     * 根据合同id获取解抵押信息
     *
     * @param dto
     * @return
     */
    @Override
    public IResponse get(MortgageDto dto) {
        try {
            Map<String, String> headers = new HashMap<>(2);
            headers.put("Authorization", this.getTokenInfo());
            ArchivedResultDto json =  this.mortgageApiFeign.get(dto, headers);
            if(1==json.getSuccess()){
                return IResponse.success(json.getData());
            }else{
                return IResponse.fail(json.getMessage());
            }
        }catch (Exception e){
            return IResponse.fail("接口异常，请联系管理员！");
        }
    }

    /**
     * 解抵押-解抵押列表查询-资产修正
     *
     * @param dto
     * @return
     */
    @Override
    public JSONObject getAfterMortgageList(MortgageDto dto) {
        Map<String, String> headers = new HashMap<>(2);
        headers.put("Authorization", this.getTokenInfo());
        return this.mortgageApiFeign.getAftermortgageList(dto, headers);
    }
}
